import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'
import { getPlatformUser, getMoneyLimit } from '@/api/user'
import { checkRoles } from '@/stores/roles'
import _ from 'lodash-es'

export const detailSellStore = () => {
    const userStore = useUserStore()
    const state = reactive({
        matchdetail: {
            show: false,
            intervalId: null,
            loading: false,
            match: {},
            txtmap: {
                soccer_handicap: {
                    ce_odds_data: '角球',
                    odds_data: '',
                    pc_odds_data: '罚牌',
                    full: '全场',
                    half: '半场',
                    M: '独赢',
                    R: '让球',
                    OU: '大小',
                    OUH: '主队进球数',
                    OUC: '客队进球数',
                    CRS: ''
                },
                basketball_handicap: {
                    ce_odds_data: '角球',
                    odds_data: '',
                    pc_odds_data: '罚牌',
                    full: '全场',
                    half: '半场',
                    section1: '第一节',
                    section2: '第二节',
                    M: '独赢',
                    R: '让球',
                    OU: '大小',
                    OUH: '篮球第一队得分',
                    OUC: '篮球第二队得分',
                    CRS: ''
                }
            },
            handicapmap: {},
            tabs: [],
            activetab: '',
            handicapmapData: [],
            handicapmapActive: {}
        },


    })


    const openmatchdetail = (match) => {
        if (match.sport_type == 'soccer') {
            state.matchdetail.handicapmap = state.matchdetail.txtmap.soccer_handicap
        } else {
            state.matchdetail.handicapmap = state.matchdetail.txtmap.basketball_handicap
        }
        state.matchdetail.loading = true
        state.matchdetail.show = true
        state.matchdetail.match = match
        state.matchdetail.tabs = []
        state.matchdetail.activetab = ''
        getmatchother(true)

        http.get('order/shortcut_key_list').then((res) => {
            let a = []
            Object.entries(res.data).forEach(([key, value]) => {
                a.push(value)
            });
            window.electron.installshortcut(JSON.stringify(a))
        })
    }

    const getmatchother = (boola) => {
        clearInterval(state.matchdetail.intervalId)
        http
            .post(`/match/get_now_odds_all_details`, {
                bc_type: state.matchdetail.match.bc_type,
                event_type: state.matchdetail.match.event_type,
                match_id: state.matchdetail.match.match_id,
                ids: state.matchdetail.match.ids,
                sport_type: state.matchdetail.match.sport_type
            })
            .then(res => {

                getDetaildata(res.data,boola)

            }).catch(() => {
                clearInterval(state.matchdetail.intervalId)
                state.matchdetail.loading = false
                state.matchdetail.intervalId = setInterval(() => getmatchother(false), 2000)
            })
    }

    const getDetaildata = (row,boola) => {
        
        let a = []
        let odds_data = row.odds_data
        let ce_odds_data = row.ce_odds_data
        let pc_odds_data = row.pc_odds_data

        Object.keys(odds_data).forEach(key => {
            let value = odds_data[key]
            let b = {
                value: key,
                name: `${state.matchdetail.handicapmap[key]}`,
                data: {}
            }
            Object.keys(value).forEach(key2 => {
                let value2 = value[key2]
                let c = {
                    value: `${key}:${key2}`,
                    name: `${state.matchdetail.handicapmap[key]}${state.matchdetail.handicapmap[key2]}`,
                    data: {}
                }
                let d = []
                value2.forEach(item => {
                    if (!isEmpty(item.odds[0].nav_data)) {
                        item.name = state.matchdetail.handicapmap[item.pk_type]
                        d.push(item)
                    }
                });
                c.data = d
                b.data[c.value] = c
            })
            a.push(b)

        });


        let ce = structure(ce_odds_data, '角球', 'C')
        Object.keys(ce).forEach(key => {
            let value = ce[key]
            a.push(value)
        })
        let pc = structure(pc_odds_data, '罚牌', 'F')
        Object.keys(pc).forEach(key => {
            let value = pc[key]
            a.push(value)
        })

        if (a.length > 0) {
            state.matchdetail.tabs = []
            a.forEach(item => {
                state.matchdetail.tabs.push({
                    value: item.value,
                    label: item.name
                })
            });
            if (boola) {
                state.matchdetail.activetab = state.matchdetail.tabs[0].value
            }
            
        } else {
            state.matchdetail.activetab = ''
        }
        state.matchdetail.handicapmapData = a
        state.matchdetail.loading = false
        
        gethandicapmapActive()

        if (state.matchdetail.show) {
            clearInterval(state.matchdetail.intervalId)
            state.matchdetail.intervalId = setInterval(() => getmatchother(false), 2000)
        } else {
            clearInterval(state.matchdetail.intervalId)
        }

    }

    const isEmpty = (obj) => {
        return Object.keys(obj).length === 0;
    }

    const structure = (row, text, type) => {
        let a = {}
        row.forEach(item => {
            if (!isEmpty(item.odds[0].nav_data)) {
                let parts = item.pk_key.split(':');
                let str = `${type}:${parts[0]}`
                if (a[str]) {
                    let c = {
                        value: `${type}:${item.pk_key}`,
                        name: `${state.matchdetail.handicapmap[parts[0]]}${text}${state.matchdetail.handicapmap[parts[1]]}`,
                        data: []
                    }
                    item.name = state.matchdetail.handicapmap[parts[1]]
                    c.data.push(item)
                    a[str].data[`${type}:${item.pk_key}`] = c
                } else {
                    let b = {
                        value: str,
                        name: `${state.matchdetail.handicapmap[parts[0]]}${text}`,
                        data: {}
                    }
                    let c = {
                        value: `${type}:${item.pk_key}`,
                        name: `${state.matchdetail.handicapmap[parts[0]]}${text}${state.matchdetail.handicapmap[parts[1]]}`,
                        data: []
                    }
                    item.name = state.matchdetail.handicapmap[parts[1]]
                    c.data.push(item)
                    b.data[`${type}:${item.pk_key}`] = c
                    a[str] = b
                }
            }
        });

        return a

    }

    const changeactivetab = (value) => {
        state.matchdetail.activetab = value
        gethandicapmapActive()
    }

    const gethandicapmapActive = () => {
        state.matchdetail.handicapmapActive = {}
        state.matchdetail.handicapmapData.forEach(item => {
            if (item.value == state.matchdetail.activetab) {
                state.matchdetail.handicapmapActive = item.data
            }
        });

    }

    onUnmounted(() => {
        state.matchdetail.show = false
        clearInterval(state.matchdetail.intervalId)
        // window.electron.deleteshortcut()
    })

    return {
        state,
        openmatchdetail,
        changeactivetab,
        isEmpty
    }
}
